package com.example.android.ouactivity.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.ouactivity.R;
import com.example.android.ouactivity.model.Comment;
import java.util.List;

/**
 * Created by 庆 on 2016/11/16.
 * 我的评论的适配器
 */

public class CommentAdapter extends RecyclerView.Adapter<CommentHolder> {
    public List<Comment> comments = null;
    public CommentAdapter(List<Comment> list){
        this.comments = list;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public CommentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comm,parent,false);
        CommentHolder commentHolder = new CommentHolder(view);
        return commentHolder;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final CommentHolder holder, final int position) {
        if(onItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onItemClick(holder.itemView,comments.get(position));
                }
            });
        }
        holder.userImage.setImageResource(R.drawable.a);
        holder.userName.setText(comments.get(position).getAuthor());
        holder.date.setText(comments.get(position).getDate());
        holder.content.setText(comments.get(position).getContent());
        if(comments.get(position).getComment() != null){
            holder.comment.setText(comments.get(position).getComment());
        }else {
            holder.comment.setHeight(0);
        }
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
        return comments.size();
    }
    //设置监听接口
    public interface OnItemClickListener{
        void onItemClick(View view , Comment comm);
    }
    //监听对象
    public OnItemClickListener onItemClickListener = null;
    //监听对象赋值
    public void setOnItemClickListener(OnItemClickListener listener){
        this.onItemClickListener = listener;
    }
}
